#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "./components/chatmessagewidget/chatmessagewidget.h"
#include "head.h" // 包含自定义的头文件
#include <QTimer>
#include <QScrollBar>
// 绘制聊天窗口标题
void MainWindow::dorweChatWindow_title()
{
    ChatWindow_titleLable = new QLabel(this);
    ChatWindow_titleLable->setGeometry(QT_SIDEBAR_WIDTH + QT_MESSAGELIST_WIDTH, 0, QT_CHATWINDOWS_WIDTH, QT_CHATWINDOWS_TITLE_HEIGHT);
    ChatWindow_titleLable->setText(chatWindow_title);
    ChatWindow_titleLable->setStyleSheet("border-bottom:1px solid #e9e9e9;padding-left:20px;color:#333333;font-size:16px;background-color:#f5f5f5;");
    ChatWindow_titleLable->hide();
}
// 聊天工具模块
void MainWindow::dorweChatWindow_tool()
{
    MessageList_Search = new QLabel(this);
    MessageList_Search->setGeometry(QT_SIDEBAR_WIDTH + QT_MESSAGELIST_WIDTH, QT_CHATWINDOWS_TITLE_HEIGHT + QT_CHATWINDOWS_WINDOW_HEIGHT, QT_CHATWINDOWS_WIDTH, QT_CHATWINDOWS_TOOL_HEIGHT);
    MessageList_Search->setStyleSheet("background-image: url(../weChat/images/charTool.png);border-top:1px solid #e9e9e9;");
    MessageList_Search->hide();
}
// 聊天窗口内容区域
void MainWindow::dorweChatWindow_window()
{
    // 创建一个滚动区域
    scrollArea = new QScrollArea(this);
    scrollArea->resize(QT_CHATWINDOWS_WIDTH, QT_CHATWINDOWS_WINDOW_HEIGHT);
    // 去掉边框
    scrollArea->setStyleSheet("QScrollArea { border: none; }");
    // 隐藏滚动条
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // 创建一个QWidget作为滚动区域的内容
    contentWidget = new QWidget();
    layout = new QVBoxLayout(contentWidget);
    // 设置固定间隙
    layout->setSpacing(10);                     // 设置内容之间的间隙
    layout->setContentsMargins(10, 10, 10, 10); // 设置内容与边界的边距
    // 禁用弹性空间
    layout->setAlignment(Qt::AlignTop); // 确保内容从顶部开始排列

    // 设置QWidget为滚动区域的小部件
    scrollArea->setWidget(contentWidget);
    // 允许内容小部件根据滚动区域的大小变化而调整大小
    scrollArea->setWidgetResizable(true);
    scrollArea->setGeometry(QT_SIDEBAR_WIDTH + QT_MESSAGELIST_WIDTH, QT_CHATWINDOWS_TITLE_HEIGHT, QT_CHATWINDOWS_WIDTH, QT_CHATWINDOWS_WINDOW_HEIGHT);
}
// 绘制聊天窗口输入框
void MainWindow::dorweChatWindow_input()
{
    textEdit = new QTextEdit(this);
    // 在类构造函数中连接信号槽
    connect(textEdit, &QTextEdit::textChanged, this, [=]()
            {
         // 临时阻塞信号防止递归触发
        textEdit->blockSignals(true);

        QString text = textEdit->toPlainText();
        if (text.length() > 20)
        {
            // 截断并保留前20个字符
            textEdit->setPlainText(text.left(20));
            // 可选：调整光标位置到末尾
            QTextCursor cursor = textEdit->textCursor();
            cursor.movePosition(QTextCursor::End);
            textEdit->setTextCursor(cursor);
        }
        // 恢复信号连接
        textEdit->blockSignals(false); 
        });

    textEdit->setGeometry(QT_SIDEBAR_WIDTH + QT_MESSAGELIST_WIDTH, QT_CHATWINDOWS_TITLE_HEIGHT + QT_CHATWINDOWS_WINDOW_HEIGHT + QT_CHATWINDOWS_TOOL_HEIGHT, QT_CHATWINDOWS_WIDTH, QT_CHATWINDOWS_INPUT_HEIGHT);
    textEdit->setStyleSheet("border: none; background-color: #f5f5f5; font-size: 15px; color: #333333;");
    textEdit->hide();
}
// 绘制发送按钮
void MainWindow::dorweChatWindow_sendBtn()
{
    // 绘制一个和微信一样的发送按钮
    sendBtn = new QPushButton(this);
    sendBtn->setText("发送(s)");
    sendBtn->setGeometry(QT_SIDEBAR_WIDTH + QT_MESSAGELIST_WIDTH + QT_CHATWINDOWS_SENDBTN_LEFT_WIDTH, QT_CHATWINDOWS_TITLE_HEIGHT + QT_CHATWINDOWS_WINDOW_HEIGHT + QT_CHATWINDOWS_TOOL_HEIGHT + QT_CHATWINDOWS_INPUT_HEIGHT, 100, 40);
    sendBtn->setStyleSheet("margin-top:10px;font-size:16px; font-weight:700; background-color: #e9e9e9; color: #07c160; border: none;");
    //
    connect(sendBtn, &QPushButton::clicked, [=]()
            {
        // 获取输入框中的文本
        charInputValue = textEdit->toPlainText();
        // 发送消息
        if (!charInputValue.isEmpty())
        {
           tcpFn->tcp_send(1003); 
           textEdit->clear();
           charInputValue.clear();
           // 延迟刷新界面
           QTimer::singleShot(100, [=]()
           {

           // 更新聊天窗口
            ChatWindowUpdate();
            });
        } });
    sendBtn->hide();
}
void MainWindow::ChatWindowUpdate()
{
    qDebug() << "ChatWindowUpdate方法内,chatWindowListDatas.size():" << chatWindowListDatas.size();
    ChatWindow_titleLable->setText(chatWindow_title);
    // 清空里面元素
    while (layout->count() > 0)
    {
        QWidget *widget = layout->itemAt(0)->widget();
        layout->removeWidget(widget);
        delete widget;
    }
    // 添加内容
    for (int i = 0; i < chatWindowListDatas.size(); ++i)
    {
        chatWindowListStruct D = chatWindowListDatas[i];
        ChatMessageWidget *TextWidget = new ChatMessageWidget(D, this);
        TextWidget->setFixedWidth(QT_CHATWINDOWS_WIDTH - 30);
        layout->addWidget(TextWidget);
    }
    ChatWindow_titleLable->show();
    MessageList_Search->show();
    textEdit->show();
    sendBtn->show();
    // 延时2秒
    QTimer::singleShot(100, [=]()
                       {
       QScrollBar *verticalScrollBar = scrollArea->verticalScrollBar();
    verticalScrollBar->setValue(verticalScrollBar->maximum()+30); });
}
void MainWindow::dorweChatWindow()
{
    dorweChatWindow_title();
    dorweChatWindow_window();
    dorweChatWindow_tool();
    dorweChatWindow_input();
    dorweChatWindow_sendBtn();
}
